<template>
  <page-header-wrapper>
    <application-search
      :filterFields="['VerifyResult']"
      @search="handleSearch"
      @reset="handleReset"
    ></application-search>
    <a-card :bordered="false">
      <div class="table-header m-b-24"></div>
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
        :scroll="{ x: true }"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <div slot="tabAction">
          <buttonSelect
            v-model="typeSelect"
            :options="[
              { value: '', label: '全部' },
              { value: '符合', label: '符合' },
              { value: '不符合', label: '不符合' },
              { value: '未审核', label: '未审核' },
            ]"
            @change="handleButtonSelectChange"
          />
        </div>
        <span slot="description" slot-scope="text">
          <ellipsis :length="24" tooltip>{{ text }}</ellipsis>
        </span>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis, buttonSelect, applicationSearch } from '@/components'
import { getProposerList } from '@/api/common'
export default {
  name: 'cityComplexInquireList',
  components: {
    STable,
    Ellipsis,
    buttonSelect,
    applicationSearch,
  },
  computed: {
    textColor() {
      return function (text) {
        const classMap = new Map([
          ['符合', 'text-success'],
          ['不符合', 'text-error'],
          ['未审核', 'text-warning'],
        ])
        return classMap.get(text)
      }
    },
  },
  data() {
    this.$eventBus.$on('districtAuditRefresh', () => {
      this.$refs.table.refresh()
    })
    return {
      typeSelect: '',
      queryParam: {
        UserID: this.$store.getters.userid,
        Mode: 'DistrictAudit',
        strContent: {},
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 80,
          customRender: (text, record, index) => <span>{index + 1}</span>,
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNo',
          width: 140,
        },
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'Proposer',
          width: 140,
        },
        {
          title: '身份证号',
          align: 'center',
          dataIndex: 'IDCard',
          width: 180,
        },
        {
          title: '区/县',
          align: 'center',
          dataIndex: 'RegionName',
          scopedSlots: { customRender: 'description' },
          width: 220,
        },
        {
          title: '社区/街道',
          align: 'center',
          dataIndex: 'TownshipName',
          scopedSlots: { customRender: 'description' },
          width: 220,
        },
        {
          title: '保障人数',
          align: 'center',
          dataIndex: 'CityVfyNumber',
          width: 140,
        },
        {
          title: '保障方式',
          align: 'center',
          dataIndex: 'ApplyClass',
          width: 140,
        },
        {
          title: '补贴方式',
          align: 'center',
          dataIndex: 'CityVfyAssign',
          width: 140,
        },
        {
          title: '区级审核状态',
          align: 'center',
          fixed: 'right',
          dataIndex: 'VerifyResult',
          customRender: (text) => {
            let value = '未审核'
            if (!this.$isEmpty(text)) {
              value = text
            }
            return <span class={this.textColor(value)}>{value}</span>
          },
          width: 140,
        },
        {
          title: '市级审核状态',
          align: 'center',
          fixed: 'right',
          dataIndex: 'CityVfyResult',
          customRender: (text) => {
            let value = '未审核'
            if (!this.$isEmpty(text)) {
              value = text
            }
            return <span class={this.textColor(value)}>{value}</span>
          },
          width: 140,
        },
        {
          title: '审批结果',
          align: 'center',
          fixed: 'right',
          dataIndex: 'CityRfyResult',
          customRender: (text) => {
            let value = '未审核'
            if (!this.$isEmpty(text)) {
              value = text
            }
            return <span class={this.textColor(value)}>{value}</span>
          },
          width: 140,
        },
        {
          title: '操作',
          align: 'center',
          fixed: 'right',
          width: 140,
          customRender: (text, record, index) => {
            const isNoSubmitToCity = ['0', '-1'].includes(record.SubmitToCity) || this.$isEmpty(record.SubmitToCity)
            return (
              <div>
                <y-button type="link" onClick={this.handleDetail.bind(this, record.ID)}>
                  查看{isNoSubmitToCity}
                </y-button>
                {isNoSubmitToCity ? (
                  <y-button type="link" onClick={this.handleAudit.bind(this, record.ID)}>
                    审核
                  </y-button>
                ) : (
                  ''
                )}
              </div>
            )
          },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        requestParameters.strContent.VerifyResult = this.typeSelect
        return getProposerList(requestParameters).then((res) => {
          return res
        })
      },
    }
  },
  methods: {
    handleSearch(values) {
      this.queryParam.strContent = values
      this.$refs.table.refresh(true)
    },
    handleReset() {
      this.queryParam.strContent = {}
      this.typeSelect = ''
      this.$refs.table.refresh(true)
    },
    handleButtonSelectChange(e) {
      this.$refs.table.refresh(true)
    },
    handleDetail(id) {
      localStorage.setItem('browserCacheDetail', JSON.stringify({ id: id }))
      this.$router.push({
        name: 'districtAuditManagementDetail',
        params: {
          id,
        },
      })
    },
    handleAudit(id) {
      this.$router.push({
        name: 'districtAuditManagementAudit',
        params: {
          id,
          type: 'district',
        },
      })
    },
  },
}
</script>
<style lang="less" scoped>
.table-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
</style>
